import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { HeroService } from '../../../../services/hero.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { differenceInCalendarDays } from 'date-fns'

import {
  FormBuilder,
  FormControl,
  FormGroup,
  Validators
} from '@angular/forms';

@Component({
  selector: 'app-signing',
  templateUrl: './signing.component.html',
  styleUrls: ['./signing.component.css']
})
export class SigningComponent implements OnInit {

  @Input() childValue: Array<any> = [];
  @Output() event = new EventEmitter();
  validateForm: FormGroup;
  today = new Date();
  isShow = true;
  btn_loading = false;
  public ksum: string;
  public csum: string;
  public msum: string;
  public ysum: string;
  public userName: string;
  public date: string;
  public tips: number;
  public radioValue = '1';
  public userlist: Array<any> = [''];
  public dateFormat = 'yyyy-MM-dd';

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
  }

  constructor(private heroService: HeroService,
    private fb: FormBuilder,
    private router: Router,
    private route: ActivatedRoute,
    private message: NzMessageService,
    private modalService: NzModalService) { }

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      user: [null, [Validators.required]],
      payType: [null, [Validators.required]],
      date: [null, [Validators.required]],
      multiple: [null, [Validators.required]],
      setMealMoney: [null, [Validators.required]],
      blackUnit: [null, [Validators.required]],
      blackPresented: [null, [Validators.required]],
      coloursUnit: [null, [Validators.required]],
      coloursPresented: [null, [Validators.required]],
      cashPledge: [null, [Validators.required]]
    });
    //获取租户列表
    let url = host + "/main/util/getUserByMerchantId";
    this.heroService.get(url).then(result => {
      if (result.code == 0) {
        if (result.data.length !== 0) {
          this.isShow = false;
        }
        this.userlist = result.data;
      } else {
        this.modalService.error({
          nzTitle: '提示',
          nzContent: result.msg
        });
      }
    })
  }

  getUserName(user) {
    this.userName = user.listOfTemplateOption[0].nzLabel;
  }

  signPrinter() {
    if (this.validateForm.status == "VALID") {
      this.btn_loading = true;
      let signedBegin = this.turnDate(this.validateForm.value.date);
      let url = host + "/main/merchantPrinter/signed";
      if (this.childValue.length > 0) {
        var model = 'userId=' + this.validateForm.value.user + '&userName=' + this.userName + '&printerIds=' + this.childValue + '&isAuto=' + this.radioValue + '&payType=' + this.validateForm.value.payType + '&multiple=' + this.validateForm.value.multiple + '&setMealMoney=' + this.validateForm.value.setMealMoney + '&blackUnit=' + this.validateForm.value.blackUnit + '&blackPresented=' + this.validateForm.value.blackPresented + '&coloursUnit=' + this.validateForm.value.coloursUnit + '&coloursPresented=' + this.validateForm.value.coloursPresented + '&cashPledge=' + this.validateForm.value.cashPledge + '&signedBegin=' + signedBegin;
        this.heroService.post(url, model).then(result => {
          if (result.code == 0) {
            this.message.success('签约成功');
            this.event.emit({ isSignVisible: false });
          } else {
            this.btn_loading = false;
            this.modalService.error({
              nzTitle: '提示',
              nzContent: result.msg
            });
          }
        })
      } else {
        let id = this.route.snapshot.paramMap.get('id');
        let signedBegin = this.turnDate(this.validateForm.value.date);
        var model = 'userId=' + this.validateForm.value.user + '&userName=' + this.userName + '&printerId=' + id + '&isAuto=' + this.radioValue + '&payType=' + this.validateForm.value.payType + '&multiple=' + this.validateForm.value.multiple + '&setMealMoney=' + this.validateForm.value.setMealMoney + '&blackUnit=' + this.validateForm.value.blackUnit + '&blackPresented=' + this.validateForm.value.blackPresented + '&coloursUnit=' + this.validateForm.value.coloursUnit + '&coloursPresented=' + this.validateForm.value.coloursPresented + '&cashPledge=' + this.validateForm.value.cashPledge + '&signedBegin=' + signedBegin;
        this.heroService.post(url, model).then(result => {
          console.log(result);
          if (result.code == 0) {
            this.message.success('签约成功');
            this.router.navigate(['/main/printer']);
          } else {
            this.btn_loading = false;
            this.modalService.error({
              nzTitle: '提示',
              nzContent: result.msg
            });
          }
        })
      }
    }
  }

  // 禁选今日之前的日期
  // disabledDate = (current: Date): boolean => {
  //   return differenceInCalendarDays(current, this.today) < 0;
  // };

  //转时间格式
  turnDate(date) {
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let mydate = year.toString() + "-" + month.toString() + "-" + day.toString();
    return mydate;
  }
  cancel() {
    this.event.emit({ isSignVisible: false });
  }
  backPrevent() {
    return false;
  }
}
